// ignore_for_file: unrelated_type_equality_checks

import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/dialog/m_addClock_dialog.dart';
import 'package:full_display/page/montage/montage_provider.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/widgets/selectItem/down_menu_item.dart';
import 'package:full_display/unit/widgets/selectItem/down_select_widget.dart';
import 'package:full_display/unit/widgets/widget_dialog/widget_dialog.dart';
import 'package:provider/provider.dart';

enum PinjieAddType {
  /// none
  none,

  /// image
  image,

  /// text
  text,
}

class MPinjieAddDialogWidget extends StatelessWidget {
  const MPinjieAddDialogWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => MontageProvider())],
      child: const MPinjieAddDialog(),
    );
  }
}

class MPinjieAddDialog extends StatefulWidget {
  const MPinjieAddDialog({super.key});

  @override
  State<MPinjieAddDialog> createState() => _MPinjieAddDialogState();
}

class _MPinjieAddDialogState extends State<MPinjieAddDialog> {
  TextEditingController nameVC = TextEditingController();
  TextEditingController rowVC = TextEditingController();
  TextEditingController columnVC = TextEditingController();

  String value = "1";

  var _montageProvider = MontageProvider();
  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    return Container(
      width: 320.0.W,
      height: 512.0.W,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter, //右上
            end: Alignment.bottomCenter, //左下
            //渐变颜色[始点颜色, 结束颜色]
            colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(10.0.W, 20.0.H, 10.0.W, 20.0.H),
            child: Column(
              children: [
                Text(
                  "添加",
                  style: StandardTextStyle.textWhite17Style,
                  textAlign: TextAlign.center,
                ),
                Container(
                  height: 10.0.H,
                ),
                pinjieAddItem("名称", PinjieAddType.none, vc: nameVC),
                pinjieAddItem("型号", PinjieAddType.image),
                pinjieAddItem("行数", PinjieAddType.text, vc: rowVC, info: "行"),
                pinjieAddItem("列数", PinjieAddType.text,
                    vc: columnVC, info: "列"),
                Container(
                  height: 15.0.H,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Text(
                      "模式",
                      style: StandardTextStyle.textWhite17Style,
                      textAlign: TextAlign.left,
                    ),
                  ],
                ),
                pinjieModelItem(_montageProvider.pinjieModel),
              ],
            ),
          ),
          Column(
            children: [
              Container(
                padding: EdgeInsets.only(top: 15.0.W),
                child: Divider(
                  height: 1.0.W,
                  color: ColorPlate.white, // 分割线颜色
                ),
              ),
              Container(
                height: 50.0.W,
                child: Row(
                  children: [
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          // widget.okBtnTap();
                          Navigator.pop(context);
                        },
                        child: Center(
                          child: Text(
                            '生成拼接',
                            style: StandardTextStyle.textWhite16Style,
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget pinjieAddItem(String name, PinjieAddType type,
      {TextEditingController? vc, String? info}) {
    return Container(
      margin: EdgeInsets.only(top: 10.0.H),
      padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 0),
      width: 280.0.W,
      height: 50.0.W,
      decoration: BoxDecoration(
        //设置边框
        border: Border.all(color: Colors.white, width: 1),
        //背景颜色
        color: ColorPlate.backgroundOpacity0,
        //设置圆角
        borderRadius: BorderRadius.circular((25.0.W)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            child: Text(
              name,
              style: StandardTextStyle.textWhite17Style,
              textAlign: TextAlign.center,
            ),
          ),
          Container(
            width: 10.0.W,
          ),
          (type == PinjieAddType.none || type == PinjieAddType.text)
              ? Expanded(
                  child: Container(
                    height: 50.0.W,
                    child: TextField(
                        style: const TextStyle(color: Colors.white),
                        textAlign: TextAlign.right,
                        controller: vc,
                        decoration: InputDecoration(
                            hintText: '请输入',
                            hintStyle: StandardTextStyle.inputHintStyle,
                            border: InputBorder.none),
                        autofocus: true,
                        cursorColor: ColorPlate.white),
                  ),
                )
              : type == PinjieAddType.image
                  ? Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(),
                          InkWell(
                            child: Row(
                              children: [
                                DownSelectWidget(
                                  items: [
                                    DownMenuItem(label: "S1", value: '1'),
                                    DownMenuItem(label: "S2", value: '2'),
                                    DownMenuItem(label: "S3", value: '3'),
                                    DownMenuItem(label: "S4", value: '4'),
                                    DownMenuItem(label: "S5", value: '5'),
                                    DownMenuItem(label: "S6", value: '6'),
                                  ],
                                  value: value,
                                  valueChanged: selectChange,
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    )
                  : Container(),
          type == PinjieAddType.text
              ? Container(
                  padding: EdgeInsets.only(left: 5.0.W),
                  child: Text(
                    info ?? "",
                    style: StandardTextStyle.textWhite17Style,
                    textAlign: TextAlign.center,
                  ),
                )
              : Container(),
        ],
      ),
    );
  }

  Widget pinjieModelItem(List list) {
    List<Widget> listView = [];
    for (Map map in list) {
      listView.add(
        Container(
          margin: EdgeInsets.only(top: 10.0.H),
          child: Row(
            children: [
              InkWell(
                onTap: () {},
                child: Image.asset(
                  height: 25.0.H,
                  width: 25.0.W,
                  map["isSelect"] == true
                      ? AssetUtils.getAsset2Image("icon_select")
                      : AssetUtils.getAsset2Image("icon_noselect"),
                ),
              ),
              Container(
                width: 15.0.W,
              ),
              Container(
                width: 50.0.W,
                height: 36.0.H,
                color: ColorPlate.D8D8D8,
              ),
              Container(
                width: 15.0.W,
              ),
              Text(
                map["title"] ?? "",
                style: StandardTextStyle.textWhite17Style,
                textAlign: TextAlign.left,
              ),
            ],
          ),
        ),
      );
    }
    return Column(
      children: listView,
    );
  }

  /// 下拉选择值改变
  selectChange(value) {
    print("值改变了：$value");
  }
}
